package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/7.
 */
@Controller
public class IndexController {
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IStrategySearchService strategySearchService;
    @RequestMapping("/strategy")
    public String strategy(Model model){
        List<StrategyStatisVO> vos = strategyStatisRedisService.getAllStrategyCommends();
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : vos) {
            //abroadCds    //海外的
            if ( vo.isIsabroad() && abroadCds.size()<10){
                abroadCds.add(vo);
            }
            //unabroadCds //不是海外的
            if (!vo.isIsabroad() && unabroadCds.size()<10){
                unabroadCds.add(vo);
            }
        }
        //hotCds      //热门地区
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.getHotStrategyCommends();
        //commends  查询5个攻略推荐
        List<StrategyCommend> commends = strategyCommendService.queryTop5();
        //主题攻略推荐
        //themeCds
        List<Map<String,Object>> themeCds = strategySearchService.queryThemesComends();
        //abroads
        List<StatisVO> abroads = strategySearchService.
                queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_ABROAD);//0
        //chinas
        List<StatisVO> chinas = strategySearchService.
                queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_UN_ABROAD);//1
        //themes
        List<StatisVO> themes = strategySearchService.
                queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_THEME);//2
        model.addAttribute("commends",commends);
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);
        model.addAttribute("hotCds",hotCds);
        model.addAttribute("themeCds",themeCds);
        model.addAttribute("chinas",chinas);
        model.addAttribute("abroads",abroads);
        model.addAttribute("themes",themes);

        return "strategy/index";
    }
    @RequestMapping("")
    public String index(Model model){
        List<TravelCommend> travelCommends = travelCommendService.queryTop5();
        model.addAttribute("tcs",travelCommends);
        List<StrategyCommend> strategyCommends = strategyCommendService.queryTop5();
        model.addAttribute("scs",strategyCommends.subList(0,1));
        return "index/index";
    }
    @RequestMapping("index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo){
        //pageInfo
        PageInfo pageInfo =travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "index/travelPageTpl";
    }
}
